package com.ytzl.itrip.controller.userinfo;

import com.ytzl.itrip.pojo.po.userinfo.ItripUser;
import com.ytzl.itrip.pojo.po.userinfo.ItripUserLinkUser;
import com.ytzl.itrip.pojo.vo.userinfo.OftenLinkUserVO;
import com.ytzl.itrip.service.userinfo.OftenLinkUserService;
import com.ytzl.itrip.util.ResultUtil;
import com.ytzl.itrip.util.ValidationToken;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Description: 本类用于常用联系人的维护
 * Created by gengshu on 2019/12/30 16:34
 */
@RestController
@Slf4j
@Api(description = "用于接口测试")
public class OftenLinkUserController {
    @Autowired
    private ValidationToken validationToken;
    @Autowired
    private OftenLinkUserService oftenLinkUserService;

    /**
     * @return
     * @Author 李国芳
     * 查询常用联系人
     */
    @PostMapping("/biz/api/userinfo/queryuserlinkuser")
    public ResultUtil queryOftenLinkUser(@RequestBody OftenLinkUserVO oftenLinkUserVO, HttpServletRequest request) {
        String tokenJsonString = request.getHeader("token");
        ItripUser currentUser = validationToken.getCurrentUser(tokenJsonString);
        try {
            List<ItripUserLinkUser> list = oftenLinkUserService.queryItripUserLinkUserById(Integer.parseInt(currentUser.getId().toString()), oftenLinkUserVO.getLinkUserName());
            return ResultUtil.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failed("查询失败", "1001");
        }

    }

    /**
     * @param oftenLinkUserVo
     * @param request
     * @return
     * @Author 张海铭
     * 添加常用联系人
     */
    @PostMapping("/biz/api/userinfo/adduserlinkuser")
    public ResultUtil addOftenLinkUser(@RequestBody OftenLinkUserVO oftenLinkUserVo, HttpServletRequest request) {

        if (StringUtils.isEmpty(oftenLinkUserVo.getLinkUserName()) || StringUtils.isEmpty(oftenLinkUserVo.getLinkIdCard()) || StringUtils.isEmpty(oftenLinkUserVo.getLinkPhone())) {
            return ResultUtil.failed("不能为空", "1001");
        }
        String tokenJsonString = request.getHeader("token");
        ItripUser currentUser = validationToken.getCurrentUser(tokenJsonString);
        try {
            oftenLinkUserService.addOftenLinkUser(oftenLinkUserVo, currentUser.getId());
            return ResultUtil.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failed("添加异常", "1002");
        }

    }

    /**
     * 修改
     * @Author 李文洁
     * @param oftenLinkUserVO
     * @return
     */
    @PostMapping("biz/api/userinfo/modifyuserlinkuser")
    public ResultUtil updateOftenLinkUser(@RequestBody OftenLinkUserVO oftenLinkUserVO) {

        try {
            Integer i = oftenLinkUserService.updateOftenLinkUser(oftenLinkUserVO);
            if (i != 0) {
                return ResultUtil.success("修改成功");
            } else {
                return ResultUtil.failed("修改失败", "1009");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failed("修改异常", "1008");
        }

    }

    /**
     * 根据id删除常用联系人
     * @Author 耿书
     * @param id
     * @return
     */
    @GetMapping("biz/api/userinfo/deluserlinkuser")
    public ResultUtil deleteLinkUserById(@RequestParam("ids") Integer id) {
        log.info("======"+id);
        try {
            boolean flag = oftenLinkUserService.deleteLinkUserById(id);
            if (flag) {
                return ResultUtil.success("删除成功！！");
            }
            return ResultUtil.failed("删除失败！！", "1011");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failed("删除操作失败！！", "1012");
        }
    }
}
